package com.pro.controller;

import com.pro.pojo.JsCarousel;
import com.pro.pojo.JsonData;
import com.pro.service.JsCarouselService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

/**
 * @Author 祖铭泽
 * @Date 2022/1/7/0007
 * @description 轮播图 控制层
 */
@Controller
@RequestMapping(value = "/banner")
public class JsCarouselController {
    @Autowired
    private JsCarouselService jsCarouselService;
    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;
    /**
     * @Author 祖铭泽
     * @Date 2022/1/7/0007
     * @description 根据上线/下线状态、图片类型获取轮播图
     */
    @RequestMapping(value = "/getBanners")
    @ResponseBody
    public JsonData getBanners(JsCarousel jsCarousel){
        return jsCarouselService.getBannersByDownType(jsCarousel);
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/1/7/0007
     * @description 根据id删除轮播图
     */
    @RequestMapping(value = "/delBanner")
    @ResponseBody
    public JsonData delBannerById(Integer carouselId){
        return jsCarouselService.delBannerById(carouselId);
    }

    /**
     * @Author 祖铭泽
     * @Date 2022/1/11/0007
     * @description 上传轮播图 图片
     */
    @RequestMapping(value = "/uploadBannerImage")
    @ResponseBody
    public JsonData uploadBannerImage(@RequestParam("image") MultipartFile file){
        if(file.getSize() > 0){
            //数据库文件路径
            String imageUrl = null;
            try {
                imageUrl = fastDFSClientUtil.uploadFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //前台传递路径
            String src = fastDFSClientUtil.getResAccessUrl(imageUrl);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("src", src);
            return map.get("src") != null ? JsonData.buildSuccess(map) : JsonData.buildError("上传失败");
        }
        return null;
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/1/11/0007
     * @description 上传轮播图 视频
     */
    @RequestMapping(value = "/uploadBannerVideo")
    @ResponseBody
    public JsonData uploadBanner(@RequestParam("video") MultipartFile file){
        if(file.getSize() > 0){
            String videoUrl = null;
            HashMap<String, Object> map = new HashMap<>();
            try {
                videoUrl = fastDFSClientUtil.uploadFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String src = fastDFSClientUtil.getResAccessUrl(videoUrl);
            System.out.println(src);

            map.put("src",src);
            map.put("code",200);
            return map.get("src") != null ? JsonData.buildSuccess(map) : JsonData.buildError("上传失败");
        }
        return null;
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/11/0007
     * @description 添加轮播图
     */
    @RequestMapping(value = "/addBanner")
    @ResponseBody
    public JsonData addBanner(JsCarousel jsCarousel){
        boolean addBanner = jsCarouselService.addBanner(jsCarousel);
        return addBanner ? JsonData.buildSuccess(jsCarousel) : JsonData.buildError("添加失败");
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/12/0007
     * @description 根据id查询轮播图
     */
    @RequestMapping(value = "/getBannerMessageById")
    @ResponseBody
    public JsonData getBannerMessageById(Integer carouselId) {
        return jsCarouselService.getBannerMessageById(carouselId);
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/12/0007
     * @description 根据id修改轮播图
     */
    @ResponseBody
    @RequestMapping(value = "/updateBannerMessage")
    public JsonData updateBannerMessage(JsCarousel jsCarousel){
        return jsCarouselService.updateBannerMessage(jsCarousel);
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/21/0007
     * @description 查询即将到期的轮播图数量
     */
    @ResponseBody
    @RequestMapping(value = "/getDueSoonBannersCount")
    public JsonData getDueSoonBannersCount() {
        return jsCarouselService.getDueSoonBannersCount();
    }
}
